package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.contract.bean.ChooseModuleBean;
import com.wwwarehouse.contract.bean.ResourceFactorBean;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/9/22.
 * 资源要素的两个adapter
 */

public class RescourceFactorAdapter extends BaseAdapter {
	private List<ChooseModuleBean.ModuleList.BeanList> list = new ArrayList<>();
	private Context context;



	public RescourceFactorAdapter(List<ChooseModuleBean.ModuleList.BeanList> list, Context context) {
		this.list = list;
		this.context = context;

	}

	@Override
	public int getCount() {
		if (list.size() > 6) {
			return 6;
		}
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.rescource_factor_item, null);
			holder = new ViewHolder();
			holder.mIv = (ImageView) convertView.findViewById(R.id.resource_factor_iv);
			holder.mTv = (TextView) convertView.findViewById(R.id.resource_factor_tv);
			holder.mEllipsisLayout = (LinearLayout) convertView.findViewById(R.id.ellipsis_layout);
			holder.mContentLayout = (LinearLayout) convertView.findViewById(R.id.content_layout);
			holder.mEllipsisLayout.setVisibility(View.GONE);
			holder.mContentLayout.setVisibility(View.VISIBLE);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (list != null && list.size()>0) {
			holder.mTv.setText(list.get(position).getName());
			if (list.get(position).getImageUrl() != null && list.get(position).getImageUrl().getSelectedUrl() != null) {
				BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getSelectedUrl() , holder.mIv);
			}
			//大于6个显示省略号
			if (list.size() > 6 && position == 5) {
				holder.mContentLayout.setVisibility(View.GONE);
				holder.mEllipsisLayout.setVisibility(View.VISIBLE);
			}

		}
		return convertView;
	}

	class ViewHolder {
		ImageView mIv;
		TextView mTv;
		LinearLayout mEllipsisLayout;
		LinearLayout mContentLayout;


	}
}